﻿using System;

namespace DDDNorthWind.Core.Domain.Identity
{
    public abstract class IdentityKey<TKey> : IIdentityKey,IEquatable<TKey>
    {
        public abstract TKey Id { get; protected set; }

        public string GetId()
        {
            return Id.ToString();
        }

        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            if (ReferenceEquals(this, obj)) return true;

            var identity = obj as IdentityKey<TKey>;

            return identity != null && Equals(identity);

            
        }

        public abstract bool Equals(TKey other);

        public override string ToString()
        {
            return string.Format("{0}-{1}", GetType().Name.Replace("Id", ""), Id);
        }

        public override int GetHashCode()
        {
            unchecked
            {
                return (Id.GetHashCode() * 397);
            }
        }
        
    }
}